{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Distributed FGSM Attack\n",
    "Adversarial attacks can be time-consuming and expensive to generate, especially when the model is large and the dataset is large or the attack is complex and requires many iterations. To minimize the time required to generate adversarial examples, `advsecurenet` supports distributed adversarial attacks. Distributed adversarial attacks utilize multiple GPUs to generate adversarial examples in parallel, which can significantly reduce the time required to generate adversarial examples. In this notebook, we demonstrate how to use distributed adversarial attacks to generate adversarial examples for a model using the Fast Gradient Sign Method (FGSM) attack."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: Create a Configuration File\n",
    "\n",
    "As a first step, you need to have a default configuration file. You can create a configuration file by running the following command:\n",
    "    \n",
    "```sh\n",
    "advsecurenet utils configs get -c fgsm_attack_config.yml -s -o ./default_fgsm_attack_config.yml\n",
    "```\n",
    "\n",
    "Here, we are also providing the `-o` flag to specify the output directory where the configuration file will be saved. You can also specify the output directory by passing the `-o` flag followed by the path to the directory where you want to save the configuration file. If you don't provide the `-o` flag, the configuration file will be saved in the current working directory with the name `fgsm_attack_config.yml`. If the file already exists, it will be overwritten. If the output path is a directory, the configuration file will be saved in that directory with the name `fgsm_attack_config.yml`.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet utils configs get -c fgsm_attack_config.yml -s -o ./default_fgsm_attack_config.yml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's check the content of the default config file\n",
    "!advsecurenet utils configs get -c fgsm_attack_config.yml -p"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Modify the Configuration File\n",
    "\n",
    "The default configuration file is capable of running the non-targeted `FGSM` attack on the `CIFAR10` dataset using the `ResNet18` model. You can modify the configuration file to run the FGSM attack on a different dataset or model. You can modify attack parameters, such as the epsilon value, and choose between a targeted or non-targeted attack. Additionally, you can select whether to use the GPU or CPU for the attack and decide whether to save the adversarial images\n",
    "\n",
    "For this example, we will modify the configuration file to run the `FGSM` attack on the `CIFAR10` dataset using the `VGG16` model. We will also add a *normalization layer* to the model, use the distributed mode for the attack, and *save the adversarial images*. We won't use the whole dataset for the attack, but only the *100 random samples*. Finally, we will set the epsilon value to *0.1*.\n",
    "\n",
    "To set the distributed mode, you need to set the `use_ddp` parameter to `True`, `processor` to `gpu`, and `gpu_ids` to the list of GPU IDs you want to use for the attack. **Ensure that the GPUs you specify are available on your system.**\n",
    "```yaml\n",
    "device:\n",
    "  use_ddp: true\n",
    "  processor: cuda\n",
    "  gpu_ids: [1,2]\n",
    "```\n",
    "\n",
    "\n",
    "You can find the updated configuration file [here](fgsm_attack_config.yml).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Run the Attack\n",
    "\n",
    "Now that you have created and modified the configuration file, you can run the attack by using the following command:\n",
    "\n",
    "```sh\n",
    "advsecurenet attack fgsm -c fgsm_attack_config.yml\n",
    "```\n",
    "\n",
    "**Note:** Attack success rate is calculated as the percentage of adversarial images that are misclassified by the model and the benign images that are correctly classified by the model. Since the attack runs on a different dataset than the one used to train the model, the attack success rate might be lower than expected."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!advsecurenet attack fgsm -c fgsm_attack_config.yml"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Congratulations! You have successfully run the Distributed FGSM Attack using a configuration file. You can now use the configuration file to run the attack with different parameters without having to pass the arguments directly to the script."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "spe",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.1.undefined"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
